package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.Counter;
import com.gxa.zhonglong.service.CounterService;
import com.gxa.zhonglong.service.HeadquartersService;
import com.gxa.zhonglong.service.SubbranchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/10/27 13:06
 * Description:
 * Version: V1.0
 */
@Api(tags ="柜台接口api")
@CrossOrigin
@RestController
@RequestMapping("/counter")
public class CounterController {

    @Autowired
    private CounterService counterService;
    @Autowired
    private SubbranchService subbranchService;
    @Autowired
    private HeadquartersService headquartersService;

    @PostMapping("/selectIdAndName")
    @ApiOperation("根据分店id查询分店所属的柜台信息(id和name)")
    public Map<String,Object> selectIdAndName(Long id) throws BusinessException {
        final List<Map<String, Object>> maps = counterService.selectIdAndName(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    @PostMapping("/selectAllById")
    @ApiOperation("根据分店id查询分店所属的柜台信息")
    public Map<String,Object> selectAllById(Long id) throws BusinessException {
        final List<Map<String, Object>> maps = counterService.selectAllById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    @PostMapping("/selectById")
    @ApiOperation("根据id查询柜台的信息")
    public Map<String,Object> selectById(Long id) throws BusinessException {
        List<Map<String,Object>> list=new ArrayList<>();
        final Map<String, Object> maps = counterService.selectById(id);
        if (maps!=null){
            final Map<String, Object> sid = subbranchService.selectById((Long) maps.get("sid"));
            if(sid!=null){
                final Map<String, Object> hid = headquartersService.selectById((Long) sid.get("hid"));
                list.add(hid);
            }
            list.add(sid);
        }
        list.add(maps);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(list);
        return jsonResult.getDataMap();
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation("添加/修改柜台信息")
    public Map<String,Object> addOrUpdate(Counter counter) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        if (counter.getId()==null){
            counterService.add(counter);
            jsonResult.setMsg("添加成功!");
        }else {
            counterService.update(counter);
            jsonResult.setMsg("修改成功!");
        }
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/del")
    @ApiOperation("根据柜台id删除柜台信息")
    public Map<String,Object> del(Long id) throws BusinessException {
        counterService.del(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功!");
        return jsonResult.getDataMap();
    }
}
